from dataclasses import asdict, dataclass
from typing import Any, Dict, Optional


@dataclass
class ModelOutput:
    """A class to represent the output of a LLM.""" ""

    text: str
    """The generated text."""
    error_code: int
    """The error code of the model inference. If the model inference is successful,
    the error code is 0."""
    incremental: bool = False
    model_context: Optional[Dict] = None
    finish_reason: Optional[str] = None
    usage: Optional[Dict[str, Any]] = None
    """Some metrics for model inference"""

    def to_dict(self) -> Dict:
        """Convert the model output to dict."""
        return asdict(self)

    @property
    def success(self) -> bool:
        """Check if the model inference is successful."""
        return self.error_code == 0